/**
 * 
 */
package com.project.tructc.MBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import com.project.entity.Account;
import com.project.entity.Property;
import com.project.entity.Property_Image;
import com.project.ryudo.SessionBean.AccountBeanRemote;
import com.project.ryudo.SessionBean.MStatBeanRemote;
import com.project.util.MessageCall;

/**
 * @author RYUDO
 *
 */
@ManagedBean(name="mStat")
@RequestScoped
public class MStat {
	
	private List LatestProperty;
	private List LatestPropertyImages;
	private int LatestPropertyLength;
	
	private List RecommendProperty;
	private List RecommendPropertyImages;
	private int RecommendPropertyLength;
	
	private List RequestUserProperty;
	private List RequestUserPropertyImages;
	
	private List<TotalPropertyView> eachPropertyTypeView;
	private List<TotalProperty>TotalProperty;
	
	private String statType = "tv";
	
	private int totalviews;
	private int totalStaff;
	private int totalUsers;
	private int totalProps;
	public MStat() throws NamingException, SecurityException, IOException {
		totalpropertyviewStat();
		totalpropertyStat();
		sitesummary();
		getRecommendProperty();
		getLatestProperty();
		
		
	}

	/**
	 * @param recommendPropertyLength the recommendPropertyLength to set
	 */
	public void setRecommendPropertyLength(int recommendPropertyLength) {
		RecommendPropertyLength = recommendPropertyLength;
	}

	/**
	 * @return the recommendPropertyLength
	 */
	public int getRecommendPropertyLength() {
		return this.RecommendProperty.size();
	}

	public void setLatestPropertyLength(int latestPropertyLength) {
		LatestPropertyLength = latestPropertyLength;
	}

	public int getLatestPropertyLength() {
		return LatestProperty.size();
	}

	public int getTotalviews() {
		return totalviews;
	}

	public void setTotalviews(int totalviews) {
		this.totalviews = totalviews;
	}

	public int getTotalStaff() {
		return totalStaff;
	}

	public void setTotalStaff(int totalStaff) {
		this.totalStaff = totalStaff;
	}

	public int getTotalUsers() {
		return totalUsers;
	}

	public void setTotalUsers(int totalUsers) {
		this.totalUsers = totalUsers;
	}

	public int getTotalProps() {
		return totalProps;
	}

	public void setTotalProps(int totalProps) {
		this.totalProps = totalProps;
	}

	public void setStatType(String statType) {
		this.statType = statType;
	}

	public String getStatType() {
		return statType;
	}

	public void setTotalProperty(List<TotalProperty> totalProperty) {
		TotalProperty = totalProperty;
	}

	public List<TotalProperty> getTotalProperty() {
		return TotalProperty;
	}

	public void setEachPropertyTypeView(List<TotalPropertyView> eachPropertyTypeView) {
		this.eachPropertyTypeView = eachPropertyTypeView;
	}

	public List<TotalPropertyView> getEachPropertyTypeView() {
		return eachPropertyTypeView;
	}

	public List getRequestUserProperty() {
		return RequestUserProperty;
	}

	public void setRequestUserProperty(List requestUserProperty) {
		RequestUserProperty = requestUserProperty;
	}

	public void setRequestUserPropertyImages(List requestUserPropertyImages) {
		RequestUserPropertyImages = requestUserPropertyImages;
	}

	public List getRequestUserPropertyImages() {
		return RequestUserPropertyImages;
	}

	public List getRecommendPropertyImages() {
		return RecommendPropertyImages;
	}

	public void setRecommendPropertyImages(List recommendPropertyImages) {
		RecommendPropertyImages = recommendPropertyImages;
	}

	public void setRecommendProperty(List recommendProperty) {
		RecommendProperty = recommendProperty;
	}

	public List getLatestPropertyImages() {
		return LatestPropertyImages;
	}

	public void setLatestPropertyImages(List latestPropertyImages) {
		LatestPropertyImages = latestPropertyImages;
	}

	public void setLatestProperty(List latestProperty) {
		LatestProperty = latestProperty;
	}

	public List getLatestProperty() throws NamingException {
		Context ctx = new InitialContext();
		MStatBeanRemote statRe = (MStatBeanRemote) ctx
				.lookup("MySweetHome/MStatBean/remote");
		this.LatestProperty = statRe.latestProperty();
		
		Iterator ite = this.LatestProperty.iterator();
		
		LatestPropertyImages = new ArrayList();
		
		while(ite.hasNext())
		{
			Property temp = (Property)ite.next();
			List ImageRe = statRe.latestPropertyImages(temp.getPropID());
			if(ImageRe.size() != 0)
			{
				Property_Image tempImage = (Property_Image)ImageRe.get(0);
				this.LatestPropertyImages.add(tempImage);
			}
			
		}
		return LatestProperty;
	}
	
	public List getRecommendProperty() throws NamingException, SecurityException, IOException
	{
		
			HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
			MAccount mAcc = (MAccount)request.getSession().getAttribute("mAccount");	
			Context ctx = new InitialContext();
			MStatBeanRemote statRe = (MStatBeanRemote) ctx
					.lookup("MySweetHome/MStatBean/remote");
			Context ctx2 = new InitialContext();
			MStatBeanRemote statvlogbyuser = (MStatBeanRemote) ctx2
					.lookup("MySweetHome/MStatBean/remote");
			
			if(mAcc.getAcc().getEmail() == null)
			{
				this.RecommendProperty = statRe.recommendProperty("most_view",null);
			} else if(statvlogbyuser.vlogbyuser(mAcc.getAcc().getAccID()).size() == 0)
			{
				this.RecommendProperty = statRe.recommendProperty("new_user",mAcc.getAcc());
			} else
			{
				this.RecommendProperty = statRe.recommendProperty("most_view_by_user",mAcc.getAcc());
			}
			
			Iterator ite = this.RecommendProperty.iterator();
			this.RecommendPropertyImages = new ArrayList();
			
			while(ite.hasNext())
			{
				Property temp = (Property)ite.next();
				List ImageRe = statRe.recommendPropertyImages(temp.getPropID());
				if(ImageRe.size() != 0)
				{
					Property_Image tempImage = (Property_Image)ImageRe.get(0);
					this.RecommendPropertyImages.add(tempImage);
				}
				
			}
		
		return this.RecommendProperty;
	}
	
	public String getUserProperty(String temp) throws NamingException, SecurityException, IOException
	{
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		if(request.getParameter("uid") == null)
		{
			MessageCall.errorMessage("User not found !", "");
			return "NotFound";
		}
		else
		{
			Context ctx = new InitialContext();
			MStatBeanRemote statRe = (MStatBeanRemote) ctx
					.lookup("MySweetHome/MStatBean/remote");
			List result = statRe.propertyByUser(Integer.parseInt(request.getParameter("uid")));
			
			if(result.size() != 0)
			{
				
				
				this.RequestUserProperty = result;
				Iterator ite = this.RequestUserProperty.iterator();
				this.setRequestUserPropertyImages(new ArrayList());
				
				while(ite.hasNext())
				{
					Property temp2 = (Property)ite.next();
					List ImageRe = statRe.propertyImageByUser(temp2.getPropID());
					if(ImageRe.size() != 0)
					{
						Property_Image tempImage = (Property_Image)ImageRe.get(0);
						this.RequestUserPropertyImages.add(tempImage);
					}
					
				}
				return "Found";
				
			}
			else
			{
				MessageCall.errorMessage("User not found !", "user_not_found");
				return "NotFound";
			}
		}
	}
	
	public void totalpropertyviewStat() throws NamingException
	{
		this.eachPropertyTypeView = new ArrayList<TotalPropertyView>();

		Context ctx = new InitialContext();
		MStatBeanRemote statRe = (MStatBeanRemote) ctx
				.lookup("MySweetHome/MStatBean/remote");
	
			int re = statRe.reportEachPropertyViews("PropForRent");
			this.eachPropertyTypeView.add(new TotalPropertyView("Property For Rent", re));
			int re2 = statRe.reportEachPropertyViews("PropForSale");
			this.eachPropertyTypeView.add(new TotalPropertyView("Property For Sale", re2));
			int re3 = statRe.reportEachPropertyViews("RoomForRent");
			this.eachPropertyTypeView.add(new TotalPropertyView("Room For Rent", re3));
	}
	
	public void totalpropertyStat() throws NamingException
	{
		Context ctx = new InitialContext();
		MStatBeanRemote statRe = (MStatBeanRemote) ctx
				.lookup("MySweetHome/MStatBean/remote");
		this.TotalProperty = new ArrayList<TotalProperty>();
			int re = statRe.reportEachPropertyTotal("PropForRent");
			this.TotalProperty.add(new TotalProperty("Property For Rent", re));
			int re2 = statRe.reportEachPropertyTotal("PropForSale");
			this.TotalProperty.add(new TotalProperty("Property For Sale", re2));
			int re3 = statRe.reportEachPropertyTotal("RoomForRent");
			this.TotalProperty.add(new TotalProperty("Room For Rent", re3));
	}
	
	public void sitesummary() throws NamingException
	{
		Context ctx = new InitialContext();
		MStatBeanRemote statRe = (MStatBeanRemote) ctx
				.lookup("MySweetHome/MStatBean/remote");
		this.totalProps = statRe.totalProps();
		this.totalStaff = statRe.totalStaff();
		this.totalUsers = statRe.totalUsers();
		this.totalviews = statRe.totalViews();
	}
}


